import { ImgUploadComponent } from './../../../../common/components/img-upload/img-upload.component'
import { Component, OnInit, ViewChild } from '@angular/core'
import { Location } from '@angular/common'
import { ServiceCommentDetailService } from './services/service-comment-detail.service'
import { ActivatedRoute, Params } from '@angular/router'
import { CommonUtil } from 'src/app/common/utils/common.util'
import { Toast } from 'ng-zorro-antd-mobile'

@Component({
  selector: 'app-service-comment-detail',
  templateUrl: './service-comment-detail.component.html',
  styleUrls: ['./service-comment-detail.component.scss'],
  providers: [Toast]
})
export class ServiceCommentDetailComponent implements OnInit {
  id = ''
  detail: any
  rate = ['icon-favor', 'icon-favor', 'icon-favor', 'icon-favor', 'icon-favor']
  score = 0
  comment = ''
  imgs = []
  anonymous = false
  disabled = false
  loading = false
  error = false

  public checkItemData = { value: 0, name: 'select', checked: false }

  @ViewChild(ImgUploadComponent)
  private imgUpload: ImgUploadComponent

  constructor(
    private location: Location,
    private activatedRoute: ActivatedRoute,
    private service: ServiceCommentDetailService,
    private utils: CommonUtil,
    private toast: Toast
  ) {}

  ngOnInit() {
    this.loading = true
    this.activatedRoute.queryParams.subscribe((params: Params) => {
      this.id = params.id
      this.getDetail(params.id)
    })
  }

  onClickLeft() {
    this.location.back()
  }
  getDetail(id) {
    this.service.getDetail(
      id,
      res => {
        setTimeout(() => {
          this.error = false
          this.loading = false
        }, 500)
        if (res.Success && res.Result) {
          let data = res.Result.Value[0]
          data.Photo = this.utils.getThumbnail(data.Photo)
          this.detail = data
        }
      },
      () => {
        setTimeout(() => {
          this.error = true
          this.loading = false
        }, 500)
      }
    )
  }
  handleReload() {
    this.error = false
    this.loading = true
    this.getDetail(this.id)
  }
  setRate(index) {
    this.score = index + 1
    for (let i = 0; i < this.rate.length; i++) {
      if (i <= index) {
        this.rate[i] = 'icon-favorfill'
      } else {
        this.rate[i] = 'icon-favor'
      }
    }
  }
  submitComment() {
    if (this.score === 0) {
      Toast.info('请点击评分')
      return false
    }

    Toast.loading('正在提交...', 0)

    this.disabled = true

    let obj: any = {
      Intent: this.detail.Intent,
      Service: this.score * 20,
      Comment: this.comment,
      Anonymous: this.anonymous
    }

    //判断是否上传图片
    if (this.imgUpload.isUpdate) {
      this.imgUpload.uploadImg(
        res => {
          console.log(res)

          //图片上传成功
          this.imgs = res

          if (this.imgs.length > 0) {
            obj.Photo = JSON.stringify([{ Index: 0, Items: this.imgs }])
          }
          this.submitCommentApi(obj)
        },
        error => {
          Toast.fail('图片上传失败')
        }
      )
    } else {
      this.submitCommentApi(obj)
    }
  }

  private submitCommentApi(obj) {
    let reqdata = {
      category: 'InvitationCategory_House1_Score',
      key: this.detail.Intent,
      snapshot: JSON.stringify(obj)
    }

    this.service.submitComment(
      reqdata,
      res => {
        this.disabled = false
        Toast.hide()
        let msg = '评价失败，请重试'
        if (res.Success && res.Result) {
          Toast.success('评价成功')
          this.location.back()
        } else {
          if (res.Message && res.Message.indexOf('|') > -1) {
            msg = res.Message.split('|')[0]
          }
          Toast.info(msg)
        }
      },
      () => {
        this.disabled = false
        Toast.hide()
        Toast.fail('网络异常，请重试')
      }
    )
  }
}
